package com.springcloud.consumercourse.controller;

import com.entity.Course;
import com.entity.ResponseResultSet;
import com.springcloud.consumercourse.myinterface.CourseOpenFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author
 * @create 2020-11-07 14:44
 */
//统一设置降级处理方法
//@DefaultProperties(defaultFallback = "fallback")
@RefreshScope   //加上配置中心后一定要加这个注解，否则无法自动刷新
@RequestMapping("/course")
@RestController
public class ConsumerController {

//    @Autowired
//    RestTemplate restTemplate;

//    @Autowired
//    DiscoveryClient discoveryClient;

    @Autowired
    CourseOpenFeignClient feignClient;

    @Value("${configtest}")
    String configtest;

    //获取服务端的地址
//    String uri = "http://localhost:8848/course/";

//    //获取地址的方法(RestTemplate+DiscoveryCLient的方式调用)
//    public String getUri(){
//        List<ServiceInstance> serviceInstances = discoveryClient.
//                getInstances("com-springcloud-coursedata");
//        ServiceInstance instance = serviceInstances.get(0);
//        return "http://"+instance.getHost()+":"+instance.getPort()+"/course/";
//    }

    //Ribbon方式调用,通过服务名调用
//    public String getUri(){
//
//        return "http://com-springcloud-coursedata/course/";
//    }



//    @RequestMapping("/{cid}")
//    public ResponseResultSet<Course> getOneCourse(@PathVariable("cid") Integer cid){
//
//        return restTemplate.getForObject(getUri()+cid, ResponseResultSet.class);
//    }
//
//    @RequestMapping("/courses")
//    public ResponseResultSet<List<Course>> getCourses(){
//        return restTemplate.getForObject(getUri()+"courses",ResponseResultSet.class);
//    }
//
//    @PostMapping("/addcourse")
//    public ResponseResultSet addCourse(Course course){
//        return restTemplate.postForObject(getUri()+"addcourse",course,ResponseResultSet.class);
//    }

    //OpenFeign的调用风格
    @GetMapping("/{cid}")
    public ResponseResultSet<Course> getOneCourse(@PathVariable("cid") Integer cid){
        return feignClient.getOneCourse(cid);
    }

    @GetMapping("/courses")
    public ResponseResultSet<List<Course>> getCourses(){
        return feignClient.getCourses();
    }

    @PostMapping("/addcourse")
    public ResponseResultSet addCourse(Course course){
        return feignClient.addCourse(course);
    }

    //指定timeout方法发生错误时的降级方法(单独指定)
//    @HystrixCommand(fallbackMethod = "timeoutBackMethod")
    //统一设置
//    @HystrixCommand
    @RequestMapping("/timeout")
    public ResponseResultSet timeOut(){
       return feignClient.timeOut();
    }

    //指定timeout方法发生错误时的降级方法(单独指定)
//    @HystrixCommand(fallbackMethod = "okBackMethod")
    //统一设置
//    @HystrixCommand
    //熔断器下高并发的测试
    @RequestMapping("/ok")
    public ResponseResultSet Ok(){
        return feignClient.Ok();
    }

    @RequestMapping("/configTest")
    public ResponseResultSet configTest(){
        return new ResponseResultSet(200,"ok",configtest);
    }

    //统一设置的异常方法
//    public ResponseResultSet fallback(){
//        ResponseResultSet resultSet = new ResponseResultSet(200,"success","fallback");
//        return resultSet;
//    }

//    public ResponseResultSet timeoutBackMethod(){
//        ResponseResultSet resultSet = new ResponseResultSet(200,"success","timeoutBackMethod");
//        return resultSet;
//    }
//
//    public ResponseResultSet okBackMethod(){
//        ResponseResultSet resultSet = new ResponseResultSet(200,"success","okBackMethod");
//        return resultSet;
//    }

//    @PostMapping("/limit")
//    public ResponseResultSet Limit(Course course,Integer pageIndex,Integer pageSize){
//        return restTemplate.postForObject(getUri()+"addcourse",course,ResponseResultSet.class);
//    }
}
